package com.my.ecommercesystem.controller;

import com.my.ecommercesystem.entity.Goods;
import com.github.pagehelper.Page;
import com.my.ecommercesystem.dto.GoodsDto;
import com.my.ecommercesystem.dto.MyMessage;
import com.my.ecommercesystem.dto.AllTypeGoods;
import com.my.ecommercesystem.service.GoodsService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class GoodsController {
    @Resource
    private GoodsService goodsService;

    public GoodsService getGoodsService() {
        return goodsService;
    }

    public void setGoodsService(GoodsService goodsService) {
        this.goodsService = goodsService;
    }

//    @GetMapping("/queryGoodsByGoodsId")
//    public ModelAndView queryGoodsByGoodsId(Integer id, HttpSession session){
//        ModelAndView modelAndView = new ModelAndView();
//        Goods goods = goodsService.queryGoodsByGoodsId(id);
//        //添加goods信息给视图
//        modelAndView.addObject("goods",goods);
//        modelAndView.setViewName("goodsdetail");
//        return modelAndView;
//    }
    @RequestMapping("/queryGoodsByGoodsId")
    public String queryGoodsByGoodsId(HttpServletRequest request, String id, HttpSession session){
        Integer cid=Integer.valueOf(id);
        Goods goods=goodsService.queryGoodsByGoodsId(cid);
        session.setAttribute("goodsdtl",goods);
        request.setAttribute("goods",goods);
        return "goodsdetail";
    }


//    @RequestMapping("/queryShopcarByUserId")
//    public String queryShopcarByUserId() {
//        return "shopcar";
//    }

    @RequestMapping("/getAllGoods")
    @ResponseBody
    public MyMessage getAllGoods(Integer size, Integer page, HttpServletRequest request, GoodsDto goodsDto) {

        Integer mypage = 1;
        Integer mysize = 3;
        MyMessage myMessage = new MyMessage();

        try {
            mypage = Integer.valueOf(page);
        } catch (Exception e) {

        }
        try {
            mysize = Integer.valueOf(size);
        } catch (Exception e) {

        }

        Long count = goodsService.queryGoodsCount(goodsDto);

        int maxpage = (int) (count % mysize == 0 ? (count / mysize) : (count / mysize + 1));

        //上限下限已经由pagehelper修正，不好用，我们自己修正
        mypage = mypage < 1 ? 1 : mypage;
        mypage = mypage > maxpage ? maxpage : mypage;


        Page<GoodsDto> list = goodsService.queryGoods(goodsDto, page, size);
        myMessage.setData(list);
        myMessage.setPage(page);
        myMessage.setMaxPage(maxpage);
        myMessage.setSize(size);
        myMessage.setCode("200");
        myMessage.setFlag(1);
        myMessage.setInfo("查询成功");


        return myMessage;

    }
    @RequestMapping("/getGoodsByTime")
    @ResponseBody
    public MyMessage getGoodsByTime(Integer size, Integer page) {

        Integer mypage = 1;
        Integer mysize = 3;
        MyMessage myMessage = new MyMessage();

        try {
            mypage = Integer.valueOf(page);
        } catch (Exception e) {

        }
        try {
            mysize = Integer.valueOf(size);
        } catch (Exception e) {

        }
        ;

        //上限下限已经由pagehelper修正，不好用，我们自己修正


        Page<GoodsDto> list = goodsService.queryGoodsByTime(page,size);
        myMessage.setData(list);
        myMessage.setPage(page);
        myMessage.setSize(size);
        myMessage.setCode("200");
        myMessage.setFlag(1);
        myMessage.setInfo("查询成功");


        return myMessage;

    }

    @RequestMapping("/getAllGoodsByKey")
    @ResponseBody
    public MyMessage getAllGoodsByKey(Integer size, Integer page, String key) {

        Integer mypage = 1;
        Integer mysize = 3;
        MyMessage myMessage = new MyMessage();

        try {
            mypage = Integer.valueOf(page);
        } catch (Exception e) {

        }
        try {
            mysize = Integer.valueOf(size);
        } catch (Exception e) {

        }

        Long count = goodsService.queryGoodsCountByKey("%"+key+"%");

        int maxpage = (int) (count % mysize == 0 ? (count / mysize) : (count / mysize + 1));

        //上限下限已经由pagehelper修正，不好用，我们自己修正
        mypage = mypage < 1 ? 1 : mypage;
        mypage = mypage > maxpage ? maxpage : mypage;


        Page<GoodsDto> list = goodsService.queryGoodsByKey("%"+key+"%", page, size);
        myMessage.setData(list);
        myMessage.setPage(page);
        myMessage.setMaxPage(maxpage);
        myMessage.setSize(size);
        myMessage.setCode("200");
        myMessage.setFlag(1);
        myMessage.setInfo("查询成功");


        return myMessage;

    }



    /**
     * 主页罗列各品类商品，限制4个
     * @param
     * @return
     */
    @RequestMapping("listAllTypeGoods")
    @ResponseBody
    public AllTypeGoods listAllTypeGoods() {
        List<Goods> list=goodsService.findOneType(1);
        List<Goods> list2=goodsService.findOneType(2);
        List<Goods> list3=goodsService.findOneType(3);
        List<Goods> list4=goodsService.findOneType(4);
        List<Goods> list5=goodsService.findOneType(5);
        List<Goods> list6=goodsService.findOneType(6);

        AllTypeGoods allTypeGoods=new AllTypeGoods();
        allTypeGoods.setVegetable(list);
        allTypeGoods.setSeafood(list2);
        allTypeGoods.setPingandbeef(list3);
        allTypeGoods.setQuickfrozen(list6);
        allTypeGoods.setVegetables(list5);
        allTypeGoods.setFowl(list4);
        return allTypeGoods;
    }
}
